$(document).ready(function () {

    //flash
    var message = $('#flash-message');
    if (message.length >0) {
        alert(message.html());
    }

    //máscaras
    $(".cep").mask("00000-000", {clearIfNotMatch: true});
    $(".data").mask("00/00/0000", {clearIfNotMatch: true});
    $(".rg").mask("00.000.000-0", {placeholder: " "});
    $(".cpf").mask("000.000.000-00", {placeholder: " "});
    $(".cnpj").mask("00.000.000/0000-00", {placeholder: "Ex: 99.999.999/9999-99", clearIfNotMatch: true});

    var celphoneMask = function (phone, e, currentField, options) {
        //alert(phone);
        return phone.replace(/\D/g, '').length === 11 ? '(00) 00000-0000' : '(00) 0000-00009';
    };

    var celphoneOptions = {
        onKeyPress: function (val, e, field, options) {
            field.mask(celphoneMask.apply({}, arguments), options);
        },
        placeholder: "Ex: (99) 9999-9999",
        clearIfNotMatch: true
    };

    $(".celphone").mask(celphoneMask, celphoneOptions);
    $(".telefone").mask(celphoneMask, celphoneOptions);


    $('input[type="reset"').click(function (event) {
        
        $("#codigo_abert").val("");
        $("#cidade").val("");
        $("#cpf_cnpj").val("");
        $("#razao").val("");
        $("#fantasia").val("");
        
        
        if($("#data_inicial")!=null){
            $("#data_inicial").val("");
        }
        if($("#data_final")!=null){
            $("#data_final").val("");
        }

        event.preventDefault();
        var uri = window.location.toString();
        if (uri.indexOf("?") > 0) {
            var clean_uri = uri.substring(0, uri.indexOf("?"));
            location.replace(clean_uri);
        }

    });
    
    
    //

    $('form.needs-confirmation').submit(function (event) {
        if (!confirm("Deseja realmente enviar seu cadastro?")) {
            event.preventDefault();
        }
    });

});